package com.citycloud.ccuap.tc.admin.permission;

import com.citycloud.ccuap.commons.json.CheckedTreeNode;
import com.citycloud.ccuap.tc.admin.model.SecRole;
import com.citycloud.ccuap.tc.admin.model.SecUser;
import com.citycloud.ccuap.tc.admin.sec.secuser.SecUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by rabbitQ on 2018/8/21.
 */
@Service
public class FuncPermissionService implements IFuncPermissionService {

    @Autowired
    SecUserService secUserService;

    /**
     * 4-判断用户是否有功能列表里的至少一个
     * @return
     */
    @Override
    public boolean hasFunctionPermissions(String userId,String projectId,List<String> permissions) {
        SecUser secUser = secUserService.getUser(userId,projectId);
        List<SecRole> secRoleList = secUser.getRoles();
        //多个角色
        boolean index = false;
        for(SecRole secRole : secRoleList){
            if(index){
                break;
            }else{
            //每个角色的功能权限树
                List<CheckedTreeNode> allFunTree = secRole.getFuncPermissions();
                for(String permission : permissions){
                  index = getChild(allFunTree,permission);
                  if(index == true) {
                      break;
                  }
                }
            }
        }
        return index;
    }

    /**
     * 判断用户是否有功能列表里所有功能的权限，内部直接调用isPermittedAll接口
     * @return
     */
    @Override
    public boolean hasAllFunctionPermissions(String userId,String projectId,List<String> permissions) {
        boolean hasAll = isPermittedAll(userId,projectId,permissions);
        return hasAll;
    }

    /**
     * 10-判断当前用户是否有指定权限
     * @param userId
     * @param permission
     * @return
     */
    @Override
    public boolean isPermitted(String userId,String projectId,String permission) {
        SecUser secUser = secUserService.getUser(userId,projectId);
        List<SecRole> secRoleList = secUser.getRoles();
        //多个角色
        boolean index = false;
        for(SecRole secRole : secRoleList){
            //每个角色的功能权限树
            List<CheckedTreeNode> allFunTree = secRole.getFuncPermissions();
            index = getChild(allFunTree,permission);
            if(index == true) {
                break;
            }
        }
        return index;
    }

    /**内部方法-递归查看子菜单中是否有指定权限*/
    public boolean getChild(List<CheckedTreeNode> children, String permission){
        boolean index = false;
        for(CheckedTreeNode node : children){
            boolean hasChild = false;
            if(node.getChildren() != null) {
                hasChild = true;
            }
            if(hasChild == true){
                //还有下一层
                node.getChildren();
                if(node.getId().equals(permission)){
                    index = true;
                    break;
                }else{
                    return getChild(node.getChildren(),permission);
                }
            }else{
                //没有下一层
                if(node.getId().equals(permission)){
                    index = true;
                    break;
                }
            }
        }
        return  index;
    }

    /**
     * 判断当前用户是否有指定所有权限
     * @return
     */
    @Override
    public boolean isPermittedAll(String userId,String projectId,Collection<String> permissions) {
        SecUser secUser = secUserService.getUser(userId,projectId);
        List<SecRole> secRoleList = secUser.getRoles();
        List<String> permissionList = new ArrayList<String>(permissions);
        //需要满足的个数计数器
        int allPermitNum = 0;
        boolean index = false;
        for(SecRole secRole : secRoleList){
            //每个角色的功能权限树
            List<CheckedTreeNode> allFunTree = secRole.getFuncPermissions();
            for(String permission : permissionList){
                index = getChild(allFunTree,permission);
                if(index == true) {
                    allPermitNum++;
                }
            }
        }
        if(allPermitNum == permissionList.size()){
            return true;
        }
        else {
            return false;
        }
    }


}
